package com.suxin.test.netty.adv;

import lombok.extern.slf4j.Slf4j;
import org.example.netty.BusiClient;
import org.example.netty.adv.vo.Message;
import org.example.netty.adv.vo.MessageHeader;
import org.example.netty.adv.vo.MessageType;

import java.util.Scanner;

/**
 * @Author: tang
 * @Description:
 * @Date 2025/1/21 09:38
 */
@Slf4j
public class NettyClient {

    public static void main(String[] args) throws Exception{

        BusiClient busiClient = new BusiClient("127.0.0.1",9999);

        new Thread(busiClient).start();

        // 可能没启动成功
        while (!busiClient.isConnect()) {
            synchronized (busiClient) {
                busiClient.wait();
            }
        }

        //  启动成功
        log.info("client runing success...");

        Scanner scanner = new Scanner(System.in);
        while (true) {
            String line = scanner.nextLine();

            MessageHeader messageHeader = new MessageHeader();
            Message message = new Message();
            message.setMessageHeader(messageHeader);
            // 业务请求消息
            if ("0".equals(line)) {
                message.setBody("这是一个业务请求消息!");
                messageHeader.setType(MessageType.SERVICE_REQ.value());
            }
            // 需要业务应答
            if ("1".equals(line)) {
                message.setBody("这是一个需要业务应答的消息!");
                messageHeader.setType(MessageType.SERVICE_RESP.value());
            }

            if ("-1".equals(line)) {
                log.info("jvm 关闭开始....");
                busiClient.close();
                System.exit(-1);
            }
            busiClient.sendMsg(message);
        }
    }

}
